package com.zhangcuishan.blog.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangcuishan.blog.pojo.Article;
import com.zhangcuishan.blog.service.ArticleService;
import com.zhangcuishan.blog.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/article")
@CrossOrigin
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping("/list")
    public Object list(@RequestParam(name = "id", required = false, defaultValue = "0") Long id,
            @RequestParam(name = "name", required = false) String name,
            @RequestParam(name = "labelled", required = false) Boolean labelled,
            @RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(name = "size", required = false, defaultValue = "10") Integer size) {
        try {
            // 改成分页查询
            Page<Article> queryPage = new Page<>(page, size);
            articleService.page(queryPage);
            return R.ok().put("list", queryPage.getRecords());
        } catch (Exception e) {
            log.error("error message : {}", e.getMessage(), e);
        }

        return R.ok().put("page", page);
    }

}
